package com.gpframework.module.controller.wages;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.wages.domain.GzMonthlyPickup;
import com.gpframework.module.wages.domain.req.GzMonthlyPickupAdd;
import com.gpframework.module.wages.domain.req.GzMonthlyPickupReq;
import com.gpframework.module.wages.domain.req.GzMonthlyPickupUpdate;
import com.gpframework.module.wages.domain.vo.GzMonthlyPickupVo;
import com.gpframework.module.wages.service.IGzMonthlyPickupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GzMonthlyPickupController
 * @Author gpframework
 * @Date 2024-01-19
 * @Version 1.0.0
 * @Description 拿货月度Controller
 */
@Validated
@Api(tags = "拿货月度")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/wages/monthlypickup" )
public class GzMonthlyPickupController extends BaseController {

    private final IGzMonthlyPickupService iGzMonthlyPickupService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-19
  * @Param gzMonthlyPickup
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('wages:monthlypickup:list')")
    @GetMapping("/list")
    public TableDataInfo<GzMonthlyPickupVo> list(GzMonthlyPickupReq gzMonthlyPickupReq)
    {
        startPage();
        QueryWrapper<GzMonthlyPickup> lqw = new QueryWrapper<GzMonthlyPickup>();

        gzMonthlyPickupReq.generatorQuery(lqw,true);
        List<GzMonthlyPickupVo> list = iGzMonthlyPickupService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-19
     * @Param gzMonthlyPickup
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('wages:monthlypickup:add')" )
    @Log(title = "添加拿货月度" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GzMonthlyPickupAdd gzMonthlyPickupAdd) {
        iGzMonthlyPickupService.remove(new QueryWrapper<GzMonthlyPickup>().eq("month", gzMonthlyPickupAdd.getMonth()));

        GzMonthlyPickup gzMonthlyPickup = Convert.convert(new TypeReference<GzMonthlyPickup>() {}, gzMonthlyPickupAdd);
        //单数金额 =单数 *单数单价
        gzMonthlyPickup.setSingularAmount(gzMonthlyPickupAdd.getSingularUnitPrice().multiply(new BigDecimal(gzMonthlyPickupAdd.getSingular())));
        //条数金额=条数*条数单价
        gzMonthlyPickup.setNumberOfItemsAndAmount(gzMonthlyPickupAdd.getNumberOfItemsPerUnitPrice().multiply(new  BigDecimal(gzMonthlyPickupAdd.getNumberOfItems())));
        //补贴=单数金额 +条数金额
        gzMonthlyPickup.setSubsidy(gzMonthlyPickup.getSingularAmount().add(gzMonthlyPickup.getNumberOfItemsAndAmount()));
        //补贴金额=补贴 *补贴单价
        gzMonthlyPickup.setSubsidyAmount(gzMonthlyPickup.getSubsidy().multiply(gzMonthlyPickupAdd.getSubsidyUnitPrice()));
        //实发合计=单数金额 +条数金额+补贴金额 + 休假补贴+扣减补贴

        BigDecimal total = gzMonthlyPickup.getSingularAmount().add(gzMonthlyPickup.getNumberOfItemsAndAmount()).add(gzMonthlyPickup.getSubsidyAmount()).add(gzMonthlyPickup.getVacationAllowance()).add(gzMonthlyPickup.getDeductionOfSubsidies());
        gzMonthlyPickup.setTotalPaidIn(total);
        gzMonthlyPickup.setDepartmentName("拿货组");
        return toAjax(iGzMonthlyPickupService.save(gzMonthlyPickup) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-19
     * @Param gzMonthlyPickup
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('wages:monthlypickup:edit')" )
    @Log(title = "修改拿货月度" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GzMonthlyPickupUpdate gzMonthlyPickupUpdate) {
        GzMonthlyPickup gzMonthlyPickup = Convert.convert(new TypeReference<GzMonthlyPickup>() {}, gzMonthlyPickupUpdate);
        //单数金额 =单数 *单数单价
        gzMonthlyPickup.setSingularAmount(gzMonthlyPickupUpdate.getSingularUnitPrice().multiply(new BigDecimal(gzMonthlyPickupUpdate.getSingular())));
        //条数金额=条数*条数单价
        gzMonthlyPickup.setNumberOfItemsAndAmount(gzMonthlyPickupUpdate.getNumberOfItemsPerUnitPrice().multiply(new  BigDecimal(gzMonthlyPickupUpdate.getNumberOfItems())));
        //补贴=单数金额 +条数金额
        gzMonthlyPickup.setSubsidy(gzMonthlyPickup.getSingularAmount().add(gzMonthlyPickup.getNumberOfItemsAndAmount()));
        //补贴金额=补贴 *补贴单价
        gzMonthlyPickup.setSubsidyAmount(gzMonthlyPickup.getSubsidy().multiply(gzMonthlyPickupUpdate .getSubsidyUnitPrice()));
        //实发合计=单数金额 +条数金额+补贴金额 + 休假补贴+扣减补贴
        BigDecimal total = gzMonthlyPickup.getSingularAmount().add(gzMonthlyPickup.getNumberOfItemsAndAmount()).add(gzMonthlyPickup.getSubsidyAmount()).add(gzMonthlyPickup.getVacationAllowance()).add(gzMonthlyPickup.getDeductionOfSubsidies());
        gzMonthlyPickup.setTotalPaidIn(total);

        gzMonthlyPickup.setDepartmentName("拿货组");
        return toAjax(iGzMonthlyPickupService.updateById(gzMonthlyPickup) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-01-19
     * @Param gzMonthlyPickup
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('wages:monthlypickup:export')" )
    @Log(title = "导出拿货月度" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GzMonthlyPickupReq gzMonthlyPickupReq) {
        QueryWrapper<GzMonthlyPickup> lqw = new QueryWrapper<GzMonthlyPickup>();
        gzMonthlyPickupReq.generatorQuery(lqw,true);
        List<GzMonthlyPickupVo> list = iGzMonthlyPickupService.listVo(lqw);
        ExcelUtil<GzMonthlyPickupVo> util = new ExcelUtil<GzMonthlyPickupVo>(GzMonthlyPickupVo. class);
        return util.exportExcel(list, "monthlypickup" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-19
     * @Param gzMonthlyPickup
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('wages:monthlypickup:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GzMonthlyPickupVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGzMonthlyPickupService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-19
     * @Param gzMonthlyPickup
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('wages:monthlypickup:remove')" )
    @Log(title = "删除拿货月度" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGzMonthlyPickupService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
